﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EntityModel;
using BusinessLayer;
using System.IO;
namespace PL___Windows_Inteface
{
    public partial class ManageCar : Form
    {

        private Car car;
        private Bitmap img;
        public ManageCar(string operation, string regnumber)
        {
            InitializeComponent();
            BL controller = new BL();
            foreach (Fuel f in controller.getFuelTypes())
            {
                fuel_cb.Items.Add(f.fuel_name);
            }
            foreach (States s in controller.getVehicleStates())
            {
                states_cb.Items.Add(s.state_name);
            }
            foreach (TypeCar t in controller.getCarTypes())
            {
                types_cb.Items.Add(t.type_name);
            }


            if (operation == "ADD")
            {

                viewimage_bt.Visible = false;
                delete_bt.Visible = false;
                save_changes_bt.Visible = false;



            }
            else if (operation == "VIEW")
            {

                add_image_bt.Visible = false;
                submit_bt.Visible = false;

                car = controller.getCar(regnumber);
                num_dors_tb.Text = car.doors_number.ToString();
                fuel_cb.Text = controller.getFuelName(Convert.ToInt32(car.Vehicle.fuel));
                states_cb.Text = controller.getStateName(Convert.ToInt32(car.Vehicle.state));
                types_cb.Text = controller.getCarTypeName(Convert.ToInt32(car.type_car));
                color_tb.Text = car.Vehicle.colour;
                price_tb.Text = car.Vehicle.price.ToString();
                reg_number_tb.Text = car.Vehicle.reg_number;
                if (car.Vehicle.v_image != null)
                {
                    Byte[] a = car.Vehicle.v_image.ToArray();
                    img = new Bitmap(Bytes2Image(a));
                }

            }
        }

        private void submit_bt_Click(object sender, EventArgs e)
        {
            Button bt = (Button)sender;
            if (bt.Text == "Submit")
            {
                car = new Car();
                car.Vehicle = new Vehicle();
                car.Vehicle.type_vehicle = 1;
            }
            car.Vehicle.reg_number = reg_number_tb.Text.ToString();


            BL controller = new BL();
            car.Vehicle.fuel = controller.getFuelId(fuel_cb.SelectedItem.ToString());
            try { car.Vehicle.price = Convert.ToInt32(price_tb.Text.ToString()); }
            catch { error_lb.Text = "The price must be number"; }
            car.Vehicle.colour = color_tb.Text.ToString();
            car.Vehicle.state = controller.getStateId(states_cb.SelectedItem.ToString());
            car.type_car = controller.getCarTypeId(types_cb.SelectedItem.ToString());
            try { car.doors_number = Convert.ToInt32(num_dors_tb.Text.ToString()); }
            catch { error_lb.Text = "The dors number must be number"; }
            Byte[] result = (Byte[])new ImageConverter().ConvertTo(img, typeof(Byte[]));
            car.Vehicle.v_image = new System.Data.Linq.Binary(result);



            if (bt.Text == "Submit")
            {
                controller.saveCar(car);
                error_lb.ForeColor = Color.Blue;
                error_lb.Text = "Car added correctly";
            }
            else
            {
                controller.updateCar(car);
                error_lb.ForeColor = Color.Blue;
                error_lb.Text = "Car updated correctly";

            }
        }

        private void delete_bt_Click(object sender, EventArgs e)
        {
            BL controller = new BL();
            try
            {
                controller.deleteCar(car);
                MessageBox.Show("Car Deleted Correctly", "Deleted", MessageBoxButtons.OK);
                this.Close();
            }
            catch (Exception ex)
            {
                error_lb.Text = ex.Message;

            }
        }

        private void add_image_bt_Click(object sender, EventArgs e)
        {


            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Title = "Select an Image to add in the Car register";
            if (dialog.ShowDialog() == DialogResult.OK)
            {

                img = new Bitmap(dialog.FileName);

            }
            viewimage_bt.Visible = true;
            add_image_bt.Visible = false;


        }

        private void viewimage_bt_Click(object sender, EventArgs e)
        {
            if (img != null) {
                (new ImageForm(img)).ShowDialog();
            }
            else
            {
                MessageBox.Show("No picture avaliable");
            }


        }

        public static Image Bytes2Image(byte[] bytes)
        {
            if (bytes == null) return null;
            //
            MemoryStream ms = new MemoryStream(bytes);
            Bitmap bm = null;
            try
            {
                bm = new Bitmap(ms);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
            return bm;
        }
        

    }
}
